// component/jthj/jthj.js
var app = getApp();
var config = require("../../utils/config.js");
var network = require("../../utils/network.js");
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // jthjData:Object,
    // jthjDataPackage:Array,
    // jthjDataCate:Array
    m:String,   //房屋面积
  },

  /**
   * 组件的初始数据
   */
  data: {
    tx_choose:null,//套餐选项
    price:0,  //价格
    jthjChange:[],  //变动的小类数据
    allChange:[], //变动后的所有数据
    jthjDataPackage:[], //首次请求大类数据
    jthjDataCate:[], //首次请求小类数据
    jthjData:[],  //首次请求所有的数据
    hoursPrice:0, //钟点工价格
    ishours:false, //是否选中钟点工
    name:'',  //钟点name
    hoursid:'',  //钟点 id
    hoursData:[],  //只在点钟点工时候用下
    url: app.globalData.url
  },
  ready(){
    
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 家庭环境维护  首次请求数据
    getJthjData: function () {
      network.request({
        url: config.host + 'api_work/packageList',
        method: "POST",
        data: { cateId: 1,m:this.data.m },
        success: (res) => {
          console.log("家庭环境数据")
          console.log(res)
          if (res.data.code == 99) {
            this.setData({
              jthjDataPackage:res.data.data.package,
              jthjDataCate: res.data.data.cate,
              jthjData:res.data.data,
              hoursData: res.data.data.cate  //客户在点击钟点工时变更更最初的数据  这里记录下
            })
          }
          this.triggerEvent("changeTx", 0)
        }
      })
    },
    changeTxHours:function(e){
      let list=this.data.jthjDataPackage
      let price=e.currentTarget.dataset.price
      let list2 = this.data.hoursData
      for (let i in list){
        list[i].status=0
      }
      this.setData({
        ishours:true,
        jthjDataPackage:list,
        hoursPrice: price,
        name:e.currentTarget.dataset.name,
        hoursid:e.currentTarget.dataset.id,
        jthjDataCate:list2

      })
      this.triggerEvent("changeTx",0)
      this.selectComponent("#hours").translate();
    },
    cancel:function(e){
      console.log(e)
      this.setData({
        ishours:e.detail
      })
    },
    removeHours:function(){
      this.setData({
        ishours: false
      })
    },
    hoursPrice:function(e){
      this.triggerEvent("changeTx",e.detail)
    },
    changeTx:function(e){
      if (this.data.ishours){
        wx.showToast({
          title: '请先取消钟点工套餐',
          icon:'none'
        })
      }else{
      console.log(e)
      let price = 0
      let packageId = e.currentTarget.dataset.id  //大套餐id
      let name=e.currentTarget.dataset.name
      let status = e.currentTarget.dataset.choose //点击项目的status
      this.triggerEvent("changeTx", price);
        if(status==0){
          status=1
        }else{
          status=0
        }
        wx.request({
          url: config.host + 'api_work/packageList',
          method: "POST",
          data: { cateId: 1, packageId: packageId, status: status, m: this.data.m},
          success: (res) => {
            console.log("家庭环境数据")
            console.log(res)
            if (res.data.code == 99) {
              this.setData({
                jthjDataCate: res.data.data.cate,
                jthjDataPackage:res.data.data.package,
              })
              for(let i in this.data.jthjDataPackage){
                if (this.data.jthjDataPackage[i].status==1){
                    price = this.data.jthjDataPackage[i].money
                }
                  this.setData({
                    price: price
                })
              }
              
              this.triggerEvent("changeTx", this.data.price)
              wx.setStorageSync("jthj",{"name":name,"money":this.data.price,"id":packageId})
            }
          }
        })
      }
    },
    //改变小的选项如果需要更换套餐发送请求
    changePackage:function(id,price,name){
      network.request({
        url: config.host + 'api_work/packageList',
        method: "POST",
        data: { cateId: 1, packageId:id,m:this.data.m,status:1},
        success: (res) => {
          console.log("家庭环境数据")
          console.log(res)
          if (res.data.code == 99) {
            this.setData({
              jthjDataCate: res.data.data.cate,
              jthjDataPackage: res.data.data.package,
            })
          }
          for (let i in this.data.jthjDataPackage) {
            if (this.data.jthjDataPackage[i].status == 1) {
              console.log(this.data.jthjDataPackage[i].money)
              this.setData({
                price: this.data.jthjDataPackage[i].money
              })
            }
          }
          this.triggerEvent("changeTx", this.data.price)
          wx.setStorageSync("jthj", { "name": name, "money": this.data.price, "id": id })
        }
      })
     
    },
    //展示选择是否选择套餐弹框
    showModalChange(i){
      wx.showModal({
        title: '建议您选择套餐更划算哦',
        content: '确定切换为最适合您的套餐',
        success: (res) => {
          if (res.confirm) {
            console.log('用户点击确定,更换套餐')
            this.setData({
              tx_choose: i,
              price: this.data.jthjChange[i].money
            })
            this.triggerEvent("changeTx", this.data.price);
            this.changePackage(this.data.jthjChange[i].id, this.data.jthjChange[i].money, this.data.jthjChange[i].packName)
          } else if (res.cancel) {
            console.log("我还是选择了取消，就是多花点钱么")
            console.log(this.data.allChange)
            let list=this.data.jthjDataPackage
            for(let i in list){
              list[i].status=0
            }
            this.setData({
              price:this.data.allChange.money,
              jthjDataPackage:list
            })
            this.triggerEvent("changeTx", this.data.price);
            wx.setStorageSync("jthj", { "money": this.data.price, "change": this.data.allChange.change, "name": "自定义套餐", "array": this.data.allChange.array})
          }
        } 
      })
    },
    //改变小选项
    changeItem:function(e){
      console.log("我要改变小的选项，宝宝辛苦了")
      console.log(e)
      let id=e.currentTarget.dataset.id   //小选项的id
      let status=e.currentTarget.dataset.status //小选项的状态
      let pid=e.currentTarget.dataset.pid  //小选项的父级栏目id
      if(status==0){
        wx.request({
          url: config.host + 'api_work/algorithm',
          method: "POST",
          data: { 
            id:1,
            cateid:id,
            package: this.data.jthjDataPackage,
            cate: this.data.jthjDataCate,
            pid:pid,
            m:this.data.m
          },
          success: (res) => {
            if (res.data.code == 99) {
              this.setData({
                jthjDataCate: res.data.data.cate,
                jthjChange: res.data.data.package,
                allChange:res.data.data,
                price:res.data.data.money,
                jthjDataPackage:res.data.data.package
              })
              this.triggerEvent("changeTx", this.data.price);
              wx.setStorageSync("jthj", { "money": this.data.price, "change": this.data.allChange.change, "name": "自定义套餐", "array": this.data.allChange.array})
              console.log("我要打印变更后的所有数据")
              console.log(this.data.allChange)
              let num1='';  //消毒除菌
              let num2='';  //其他套餐
              for (let i in this.data.jthjChange){
                if (this.data.jthjChange[i].status == 2){
                  num1=i
                } else if (this.data.jthjChange[i].status == 1){
                  if (this.data.tx_choose != i) {
                    num2=i
                  }
                }
              }
              if(num1!=''){  //包含消毒除菌  强制客户选择消毒除菌套餐
                this.showModalChange(num1)
              } else if(num2!=''){
                this.showModalChange(num2)
              }
            }

          }
        })
      }
    }

   
  }
})
